package online.longzipeng.householdbillssystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import online.longzipeng.householdbillssystem.business.dto.user.UserQueryResponseDTO;
import online.longzipeng.householdbillssystem.business.entity.Bill;
import online.longzipeng.householdbillssystem.business.entity.User;
import online.longzipeng.householdbillssystem.business.vo.querey.QueryVO;
import online.longzipeng.householdbillssystem.common.exception.AppException;
import online.longzipeng.householdbillssystem.common.result.ResponseEnum;
import online.longzipeng.householdbillssystem.common.utils.ExceptionUtils;
import online.longzipeng.householdbillssystem.common.utils.Md5Utils;
import online.longzipeng.householdbillssystem.mapper.BillMapper;
import online.longzipeng.householdbillssystem.mapper.UserMapper;
import online.longzipeng.householdbillssystem.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzp
 * @since 2021-04-17
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BillMapper billMapper;

    @Override
    public boolean add(User user) {
        if (log.isDebugEnabled()) {
            log.info("用户正在注册："+user);
        }
        // 通过username判断该用户是否存在
        QueryWrapper<User> qwUser = new QueryWrapper<>();
        qwUser.eq("user_name",user.getUserName());
        List<User> users = userMapper.selectList(qwUser);
        if (!CollectionUtils.isEmpty(users)) {
            throw new AppException(ResponseEnum.USER_EXISTED);
        }
        // 对密码进行加密
        user.setPassword(Md5Utils.encrypt(user.getPassword()));
        return save(user);
    }

    @Override
    public boolean delete(Integer id) {
        // 先删除与用户相关的账单，再删除用户
        QueryWrapper<Bill> qwBill = new QueryWrapper<>();
        qwBill.eq("user_id",id);
        List<Bill> bills = billMapper.selectList(qwBill);
        List<Integer> ids = bills.stream().map(Bill::getId).collect(Collectors.toList());
        ids.forEach(idt -> billMapper.deleteById(idt));
        return removeById(id);
    }

    @Override
    public boolean edit(User user) {
        return updateById(user);
    }

    @Override
    public User findById(Integer id) {
        return getById(id);
    }

    @Override
    public User login(User user) {
        QueryWrapper<User> qwUser = new QueryWrapper<>();
        qwUser.eq("user_name",user.getUserName());
        List<User> users = userMapper.selectList(qwUser);
        if (!CollectionUtils.isEmpty(users)) {
            User loginUser = users.get(0);
            if (Md5Utils.checkPassword(user.getPassword(),loginUser.getPassword())) {
                return loginUser;
            }
        }
        throw new AppException(ResponseEnum.USER_LOGIN_ERROR);
    }

    @Override
    public void findUserExistsByUserName(String userName) {
        ExceptionUtils.isNotNull(userName,"用户名不能为空！！");
        QueryWrapper<User> qwUser = new QueryWrapper<>();
        qwUser.eq("user_name",userName);
        List<User> users = userMapper.selectList(qwUser);
        ExceptionUtils.isNull(users,"该用户名已存在！！");
    }

    @Override
    public IPage<UserQueryResponseDTO> getListPage(QueryVO queryVO) {
        Page<UserQueryResponseDTO> users = new Page<>(queryVO.getPage(),queryVO.getSize());
        return userMapper.getListPage(users,queryVO);
    }
}
